package net.oschina.app;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.robotium.solo.Solo;

/**
 * Created by Administrator on 2018/3/19.
 * <p>
 * 把重复的代码封装起来，让测试类继承此父类，使代码看起来简洁
 */

public class BasicTest extends ActivityInstrumentationTestCase2 {
    protected Solo solo1;
    private SoloWrap solo;
    protected Helper helper;
    private boolean isNeedLogin = false;

    public BasicTest() {
        super(AppStart.class);
    }

    public void setUp(boolean isNeedLogin) throws Exception {
        this.isNeedLogin = isNeedLogin;
        this.setUp();
    }


    @Override
    public void setUp() throws Exception {
        super.setUp();
        try {
            solo1 = new Solo(getInstrumentation(), getActivity());
            solo = new SoloWrap(solo1);
            helper = new Helper(solo);
            //需要登录的情况
            if (isNeedLogin) {
                helper.getPageNavvgation().clickMeTab();
                if (!helper.getPageCommon().waitForText("aotu12345aotu", 5000)) {
                    helper.getPageMyTab().clickLoginIcon();
                    helper.getPageLogin().login("735723619@qq.com", "12345678");
                }
                //不需要登陆的情况
            } else {
                helper.getPageNavvgation().clickMeTab();
                if (helper.getPageCommon().waitForText("aotu12345aotu", 5000)) {
                    helper.getPageMyTab().clickSettingIcon();
                    helper.getPageSettings().clickLoginOut();
                }

            }
            helper.getPageNavvgation().clickNewsTab();
        } catch (Exception e) {
            Log.i("Auto", "setUp出现异常了，这个异常是：" + e.getMessage());
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw e;
        }
    }


    @Override
    protected void runTest() throws Throwable {
        try {
            super.runTest();
        } catch (Throwable th) {
            Log.i("Auto", "runTest出现异常了，这个异常是：" + th.getMessage());
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw th;
        }
    }


    @Override
    public void tearDown() throws Exception {
        try {

        } catch (Exception e) {
            Log.i("Auto", "tearDown()出现异常了，这个异常是：" + e.getMessage());
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw e;
        } finally {
            solo1.finishOpenedActivities();
            super.tearDown();
        }
    }
}
